package com.fugood.mybigday.db;




import com.fugood.mybigday.db.SqlConstant.Attendance;
import com.fugood.mybigday.db.SqlConstant.ContactLog;
import com.fugood.mybigday.db.SqlConstant.GuestAttendLink;
import com.fugood.mybigday.db.SqlConstant.GuestInfo;
import com.fugood.mybigday.db.SqlConstant.GuestInfoExt;
import com.fugood.mybigday.db.SqlConstant.GuestTagLink;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;



public class DbAdapter {
	
	private static final String TAG ="DbAdapter"; 
	private DatabaseHelper mDbHelper;
    public SQLiteDatabase mDb;
	private final Context mCtx;
    
	private static final String DATABASE_NAME = "mybigday.db";
	private static final int DATABASE_VERSION = 1;
	
	
	
	
	
	private class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

        	db.execSQL(GuestInfo.CREATETABLE);
        	db.execSQL(GuestInfoExt.CREATETABLE);
        	db.execSQL(GuestTagLink.CREATETABLE);
        	db.execSQL(Attendance.CREATETABLE);
        	db.execSQL(GuestAttendLink.CREATETABLE);
        	db.execSQL(ContactLog.CREATETABLE);
        	
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        	
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            
            db.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(db);
        }
    }

	
	
	
	public DbAdapter(Context ctx) {
        this.mCtx = ctx;
    }
	
	public DbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();        
        return this;
    }
    
    public void close() {
        mDbHelper.close();
    }
    
    
    public long InsertTableRow(String TABLE, ContentValues args){
    	
        return mDb.insert(TABLE, null, args);
    }
    
    public boolean UpdateTableRow(String TABLE, String rowIdColumnName, long rowId, ContentValues args) {
    	
        return mDb.update(TABLE, args, rowIdColumnName + "=" + rowId, null) > 0;
    }
    
    
    
    
    
    //////////////////////////////////////Table Insert and Update//////////////////////////////////////////
    
    public long InsertGuestInfo(long ID, String Guest_id, String NickName, String FirstName, String FamilyName, byte[] Photo,
    				String Gender, String Address, String Zip_code, String Receiver, String FACEBOOK_id,
    				String Name_Form, String Memorandum, String FACEBOOK_username){
    	
        ContentValues args = new ContentValues();
        if(ID != -1){
        	args.put(GuestInfo.ID, ID);	
        }
        
        args.put(GuestInfo.Guest_id, Guest_id);
        args.put(GuestInfo.NickName, NickName);
        args.put(GuestInfo.FirstName, FirstName);
        args.put(GuestInfo.FamilyName, FamilyName);        
        args.put(GuestInfo.Photo, Photo);
        args.put(GuestInfo.Gender, Gender);
        args.put(GuestInfo.Address, Address);
        args.put(GuestInfo.Zip_code, Zip_code);
        args.put(GuestInfo.Receiver, Receiver);
        args.put(GuestInfo.FACEBOOK_id, FACEBOOK_id);
        args.put(GuestInfo.Name_Form, Name_Form);
        args.put(GuestInfo.Memorandum, Memorandum);
        args.put(GuestInfo.FACEBOOK_username, FACEBOOK_username);        
        
        
        return mDb.replace(GuestInfo.TABLENAME, null, args);
    }
    
    
    
    public long InsertGuestInfoExt(long UID, String Guest_id, int Type, String Title, String Value){
    	
        ContentValues args = new ContentValues();
        if(UID != -1){
        	args.put(GuestInfoExt.UID, UID);	
        }
        
        args.put(GuestInfoExt.Guest_id, Guest_id);
        args.put(GuestInfoExt.Type, Type);
        args.put(GuestInfoExt.Title, Title);
        args.put(GuestInfoExt.Value, Value);
        
        return mDb.replace(GuestInfoExt.TABLENAME, null, args);
    }
    
    public long InsertGuestTagLink(long UID, String Guest_id, String TypeName){
    	
        ContentValues args = new ContentValues();
        if(UID != -1){
        	args.put(GuestTagLink.UID, UID);	
        }
        
        args.put(GuestTagLink.Guest_id, Guest_id);
        args.put(GuestTagLink.Type_Name, TypeName);        
        
        return mDb.replace(GuestTagLink.TABLENAME, null, args);
    }	
    
    
    
    
    
    ///////Delete
    
    public void DeleteGuest(long id){
    	mDb.delete(GuestInfoExt.TABLENAME, GuestInfoExt.Guest_id+"="+id, null);
    	mDb.delete(GuestInfo.TABLENAME, GuestInfo.ID+"="+id, null);
    	
    }
	

}
